using System.Web.Mvc;
using RMH.Entities;

namespace RMH.Web.Models.Binders
{
    public class PersonBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            Person person = new Person();
            return BindModel(controllerContext, bindingContext, person);
        }

        public Person BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Person person)
        {
            var form = controllerContext.HttpContext.Request.Form;

            person.FirstName = form["FirstName"];
            person.MiddleName = form["MiddleName"];
            person.LastName = form["LastName"];
            person.PhoneNumber = form["PhoneNumber"];
            person.PhoneNumberAlternate = form["PhoneNumberAlternate"];
            person.VehicleMake = form["VehicleMake"];
            person.VehicleModel = form["VehicleModel"];
            person.VehiclePlateNumber = form["VehiclePlateNumber"];
            person.VehiclePlateState = form["VehiclePlateState"];
            person.EmailAddress = form["EmailAddress"];
            var address = person.Address;
            if (address == null)
            {
                address = new Address();
                person.Address = address;
            }
            
            address.Street1 = form["AddressStreet1"];
            address.Street2 = form["AddressStreet2"];
            address.Street3 = form["AddressStreet3"];
            address.City = form["AddressCity"];
            address.State = form["AddressState"];
            address.PostalCode = form["AddressPostalCode"];

            return person;
        }
    }
}